import { DataTypes, Model, Optional } from 'sequelize';
import sequelize from '../utils/database';
import Product from './Product';

// 定义SKU属性接口
interface ProductSkuAttributes {
  id: number;
  productId: number;
  skuCode?: string;
  specifications?: object;
  specificationText?: string;
  price: number;
  stockQuantity: number;
  salesVolume: number;
  image?: string;
  barcode?: string;
  status: number;
  createdAt?: Date;
  updatedAt?: Date;
}

// 定义创建SKU时的可选属性
interface ProductSkuCreationAttributes extends Optional<ProductSkuAttributes, 'id' | 'createdAt' | 'updatedAt'> {}

// 定义SKU模型类
class ProductSku extends Model<ProductSkuAttributes, ProductSkuCreationAttributes> implements ProductSkuAttributes {
  public id!: number;
  public productId!: number;
  public skuCode?: string;
  public specifications?: object;
  public specificationText?: string;
  public price!: number;
  public stockQuantity!: number;
  public salesVolume!: number;
  public image?: string;
  public barcode?: string;
  public status!: number;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;

  // 关联属性
  public readonly product?: Product;
}

// 初始化SKU模型
ProductSku.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    productId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '商品ID',
      field: 'product_id',
    },
    skuCode: {
      type: DataTypes.STRING(50),
      allowNull: true,
      unique: true,
      comment: 'SKU编码',
      field: 'sku_code',
    },
    specifications: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '规格组合',
    },
    specificationText: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: '规格文本描述',
      field: 'specification_text',
    },
    price: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      comment: '价格',
    },
    stockQuantity: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '库存',
      field: 'stock_quantity',
    },
    salesVolume: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '销量',
      field: 'sales_volume',
    },
    image: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'SKU图片',
    },
    barcode: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: '条形码',
    },
    status: {
      type: DataTypes.TINYINT,
      allowNull: false,
      defaultValue: 1,
      comment: '状态：0-禁用，1-启用',
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: '创建时间',
      field: 'created_at',
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: '更新时间',
      field: 'updated_at',
    },
  },
  {
    sequelize,
    tableName: 'product_skus',
    timestamps: true,
    underscored: true,
    indexes: [
      {
        fields: ['product_id'],
      },
      {
        fields: ['status'],
      },
    ],
  }
);

// 定义关联关系
ProductSku.belongsTo(Product, {
  foreignKey: 'productId',
  as: 'product',
});

Product.hasMany(ProductSku, {
  foreignKey: 'productId',
  as: 'skus',
});

export default ProductSku;